/**
 * Filename: YNBannerWebActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/18 1:03
 */
package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNShopDetail;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNShopDetailResponse;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;

public class TimeGoodsShopDetailActivity extends BaseActivity implements View.OnClickListener {
    private TextView title_top;
    private SimpleDraweeView shop_detail_pic;
    private int goodsid;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_find_timegood_shop_detail);
        context = TimeGoodsShopDetailActivity.this;
        goodsid = getIntent().getIntExtra("goodsID", 0);
        initView();
        initShopDetail(String.valueOf(goodsid));
    }

    private void initView() {
        findViewById(R.id.shop_detail_back).setOnClickListener(this);
        findViewById(R.id.do_shop).setOnClickListener(this);
        title_top = (TextView) findViewById(R.id.title_top);
        shop_detail_pic = (SimpleDraweeView) findViewById(R.id.shop_detail_pic);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shop_detail_back:
                finish();
                break;
            case R.id.do_shop:
                switch (goodsid) {
                    case 1:
                        MainActivity.goodsid = 1;
                        context.startActivity(new Intent(context, TimeGoodsAlbumActivity.class));
                        break;
                    case 2:
                        MainActivity.goodsid = 2;
                        context.startActivity(new Intent(context, TimeGoodsAlbumActivity.class));
                        break;
                    case 3:
                        MainActivity.goodsid = 3;
                        context.startActivity(new Intent(context, TimeGoodsAlbumActivity.class));
                        break;
                    case 4:
                        MainActivity.goodsid = 4;
                        context.startActivity(new Intent(context, TimeGoodsAlbumActivity.class));
                        break;
                }
                break;
        }

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }



    private void initShopDetail(String goodsID) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("goodsID", goodsID);
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynGetGoodsIntroduce))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNShopDetailResponse ynShopDetailResponse = gson.fromJson(s, YNShopDetailResponse.class);
                        if (ynShopDetailResponse != null && ReqUtils.isSuccess(ynShopDetailResponse.getCode())) {
                            YNShopDetail ynShopDetail = ynShopDetailResponse.getData().get(0);
                            title_top.setText(ynShopDetail.getName());
                            String paths = YNUrlHelper.getHomeImgUrl(context, ynShopDetail.getEbIntroduceLongPic());
                            Uri uri = Uri.parse(paths);
                            ImageOptHelper.showFrescoHome(uri, shop_detail_pic, context);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
